<?php
class PloyPinPrizesController extends AppController 
{
	public $name = 'PloyPinPrizes';
	public $uses=array('PloyPinGame','PloyPinPrizeList','PloyPinGenerate');
	public $per_page = 10;
	public $status_arr = array('online'=>'已上线','offline'=>'未上线','none'=>'不设奖','suspend'=>'暂停');
	public $local_codes = array();
/**
 * [index description]
 * @return [type] [description]
 */
	public function index(){
		App::import('Vendor', 'page');
		$totalcount = $this->PloyPinGame->find('count',array('conditions' => array('pin_brand_id'=>$_GET['pin_brand_id'])));
		$page= new Page($totalcount,$this->per_page);
		/** @var [array] [返回大奖详情] */
		$list = $this->PloyPinGame->find('all',array('conditions' => array('pin_brand_id' => $_GET['pin_brand_id']),'order'=>array('award_level asc'), 'limit'=>$page->firstRow.','.$page->listRows));
		$provinceArr=$this->provinceList();
		$this->set('totalcount',$totalcount);//总记录
		$this->set('page',$page->show());
		$this->set('list', $list);
		$this->set('status_arr', $this->status_arr);
		$this->set('provinceArr', $provinceArr);
	}
	/**
	 * [addProvince description]
	 * 添加品牌下开奖的省份
	 */			
	public function addProvince()
	{
		
		if(!empty($this->data)){
			//添加pinCode
			$this->data['PloyPinGenerate']['pin_key']=$this->PloyPinGenerate->generate_secret();
			if($this->PloyPinGenerate->save($this->data)){
				
				$message = '添加PINCODE成功！';
				$this->flash($message, 'index');
			}else{
				$message = '添加PINCODE出错！';
				$this->flash($message, 'index',3,'error');
			}
			
			exit();
		}
		$this->set('brand',$_GET['brandid']);
		// $this->set('batchs',$this->PloyPinGenerate->batchList());
	}
	/**
	 * [editAward description]
	 * 编辑每个品牌的总奖级，每个奖级的奖品情况
	 * @return [type] [description]
	 */
	public function editAward()
	{
		$awardList = $this->PloyPinGame->find('all',array('conditions' => array('pin_brand_id' => $_GET['pin_brand_id']),'order'=>array('award_level asc')));//该品牌已设置奖项列表
		var_dump($awardList);
		// var_dump($_GET['pin_brand_id']);
		exit;
	}
	/**
	 * [addAward description]
	 * 如果该品牌下面还没有奖项，则添加设置奖项
	 */
	public function addAward()
	{
		if(!empty($this->data)){
			foreach ($_POST['award_name_arr'] as $key => $value) {
				$this->data['PloyPinPrize']['award_name']=$value;
				$this->data['PloyPinPrize']['award_level']=$key+1;
				$this->data['PloyPinPrize']['award_total_num']=$_POST['award_total_num_arr'][$key];
				if (isset($this->data['PloyPinPrize']['batch_begin'])) {
					$this->data['PloyPinPrize']['batch_section']=$this->data['PloyPinPrize']['batch_begin'].'_'.$this->data['PloyPinPrize']['batch_end'];
					unset($this->data['PloyPinPrize']['batch_begin']);
					unset($this->data['PloyPinPrize']['batch_end']);
				}
				$this->data['PloyPinGame']=$this->data['PloyPinPrize'];
				$this->PloyPinGame->save($this->data);//保存到ploy_pin_game表
			}
		}
		$batchs=$this->PloyPinGenerate->batchList();
		$this->set('batchs',$batchs);
	}
	/**
	 * [addProvinceAward description]
	 * 添加该品牌下各省份各等级的奖品数量
	 */
	public function addProvinceAward()
	{
		$pin_brand_id=(empty($this->data))?$_GET['pin_brand_id']:$this->data['PloyPinPrizeList']['pin_brand_id'];
		$awardInfo = $this->PloyPinGame->find('all',array('conditions' => array('pin_brand_id' => $pin_brand_id),'order'=>array('award_level asc')));//该品牌已设置奖项列表
		$provinceList=$this->provinceList();
		if(!empty($this->data)){
			$this->PloyPinPrizeList->begin();
            $transaction = 1;
			foreach ($_POST['detail'] as $ProvinceKey => $ProvinceVal) {//遍历省份
				foreach ($ProvinceVal as $LevelKey => $LevelValue) {
					$this->data['PloyPinPrizeList']['province_id']=$ProvinceKey;
					$this->data['PloyPinPrizeList']['award_level']=$LevelKey;
					$this->data['PloyPinPrizeList']['award_num']=$LevelValue['award_num'];
					$this->data['PloyPinPrizeList']['weight']=$LevelValue['weight'];
					//calc awardNum begin
					$awardInfoKey=$LevelKey-1;
					$participateNum=$awardInfo[$awardInfoKey]['PloyPinGame']['produce_num']*0.05;//预计参加活动的人数是5%;
					$averageNum=$participateNum/$awardInfo[$awardInfoKey]['PloyPinGame']['award_total_num'];//计算出平均多少个中会有一个中奖.
					if ($LevelValue['weight']>0) {
						$averageNum=intval($averageNum/$LevelValue['weight']);
					}else
					{
						$LevelValue['weight']=-($LevelValue['weight']);//转成正数
						$averageNum=intval($averageNum*$LevelValue['weight']);
					}
					$tailNum10=mt_rand(1,$averageNum);
					$tailNum28=$this->PloyPinGenerate->dec2b28($tailNum10);
					//calc awardNum end
					$this->data['PloyPinPrizeList']['tail_num']=$tailNum28;
					$this->PloyPinPrizeList->create();
					$saveStatus=$this->PloyPinPrizeList->save($this->data);
					if (!$saveStatus) {
						$transaction = 0;
					}
				}
			}
			if ($transaction) {
				$this->PloyPinPrizeList->commit();//事务提交
			}
		}
		$this->set('pin_brand_id',$pin_brand_id);
		$this->set('provinceList',$provinceList);
		$this->set('awardInfo',$awardInfo);
	}
	/**
	 * [awardList description]
	 * 该品牌的中奖列表
	 */
	public function awardList()
	{

	}
	/**
	 * author
	 * [awardIndex description]
	 * 具体每个省份奖项详情列表
	 */
	public function awardIndex()
	{
			$list = $this->PloyPinPrize->find('all',array('conditions' => array('brand_id' => $_GET['brandid'],'province_id'=>$_GET['province_id']),'order'=>array('prize_id desc')));
			$provinceArr=$this->provinceList();
			$this->set('list', $list);
			$this->set('provinceArr', $provinceArr);
			$this->set('status_arr', $this->status_arr);
	}
	public function add(){
		if(!empty($this->data)){
			//添加pinCode
			$this->data['PloyPinGenerate']['pin_key']=$this->PloyPinGenerate->generate_secret();
			if($this->PloyPinGenerate->save($this->data)){
				
				$message = '添加PINCODE成功！';
				$this->flash($message, 'index');
			}else{
				$message = '添加PINCODE出错！';
				$this->flash($message, 'index',3,'error');
			}
			exit();
		}
		$this->set('brands',$this->brandList());
		$this->set('batchs',$this->PloyPinGenerate->batchList());
	}
	/**
	 * [provinceList description]
	 * 
	 * @return [array] [派奖区域]
	 */
	private function provinceList(){
		return array(
			0=>"北京市",1=>"天津市",
			// 2=>"河北省",3=>"山西省",4=>"内蒙古自治区",5=>"辽宁省",6=>"吉林省",7=>"黑龙江省",8=>"上海市",9=>"江苏省",10=>"浙江省",
			// 11=>"安徽省",12=>"福建省",13=>"江西省",14=>"山东省",15=>"河南省",16=>"湖北省",17=>"湖南省",18=>"广东省",19=>"广西壮族自治区",20=>"海南省",
			// 21=>"重庆市",22=>"四川省",23=>"贵州省",24=>"云南省",25=>"西藏自治区",26=>"陕西省",27=>"甘肃省",28=>"青海省",29=>"宁夏回族自治区",30=>"新疆维吾尔自治区",
			// 31=>"其他地区"
			);//将32=>"香港特别行政区",33=>"澳门特别行政区"归入其他地区
	}

}
?>